package com.rich.dmr.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.rich.dmr.R;
import com.rich.dmr.model.ChannelModel;
import com.rich.dmr.utils.ConvertUtils;
import com.rich.dmr.utils.DataBaseUtils;
import java.util.List;
import rich.dmr.DmrManager;

public class RxFrequencyGridAdapter extends RecyclerView.Adapter<RxFrequencyGridAdapter.LinearViewHolder> {
    private static String TAG = "rich---FrequencyGridAdapter";
    private Context mContext;
    private RxFrequencyGridAdapter.OnItemClickListener mListener;
    private List<ChannelModel> channelModels;
    private List<String> list;
    private DmrManager dmrManager;
    private int freType;

    @SuppressLint("WrongConstant")
    public RxFrequencyGridAdapter(Context mContext, List<String> list, int type) {
        this.mContext = mContext;
        this.list = list;
        freType = type;
        dmrManager = (DmrManager) mContext.getSystemService("dmrservice");
    }
    @SuppressLint("WrongConstant")
    public RxFrequencyGridAdapter(Context mContext, int type) {
        this.mContext = mContext;
        freType = type;
        dmrManager = (DmrManager) mContext.getSystemService("dmrservice");
        channelModels = DataBaseUtils.getInstance(mContext).findChannelList();
    }

    public void  getList(){
        channelModels = DataBaseUtils.getInstance(mContext).findChannelList();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RxFrequencyGridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        return new RxFrequencyGridAdapter.LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_rv_frequency_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RxFrequencyGridAdapter.LinearViewHolder holder, int position) {
        if(freType == 3){
            holder.frequencyNo.setText(String.valueOf(ConvertUtils.div(channelModels.get(position).getRxChannel(),1000000)));
            if (dmrManager.getReceiveFrequency() == channelModels.get(position).getRxChannel()) {
                holder.frequencyOnline.setBackgroundResource(R.drawable.shape_online);
            } else {
                holder.frequencyOnline.setBackgroundResource(R.drawable.shape_setting_offline);
            }
        }else {
            String frequency = list.get(position);
            holder.frequencyNo.setText(frequency);
            if (freType == R.string.digital_receiver_frequency) {
                if (dmrManager.getReceiveFrequency() == Integer.valueOf(frequency)) {
                    holder.frequencyOnline.setBackgroundResource(R.drawable.shape_online);
                } else {
                    holder.frequencyOnline.setBackgroundResource(R.drawable.shape_setting_offline);
                }
            } else {
                if (dmrManager.getTransmitFrequency() == Integer.valueOf(frequency)) {
                    holder.frequencyOnline.setBackgroundResource(R.drawable.shape_online);
                } else {
                    holder.frequencyOnline.setBackgroundResource(R.drawable.shape_setting_offline);
                }
            }
        }
        if (mListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mListener.ItemClickListener(holder.itemView, pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mListener.ItemLongClickListener(holder.itemView, pos);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(freType == 3){
            return channelModels.size();
        }
        return list.size();
    }

    public interface OnItemClickListener {
        void ItemClickListener(View view, int position);

        void ItemLongClickListener(View view, int position);
    }

    public void setOnClickListener(RxFrequencyGridAdapter.OnItemClickListener listener) {
        this.mListener = listener;
    }

    class LinearViewHolder extends RecyclerView.ViewHolder {
        private TextView frequencyNo;
        private TextView frequencyOnline;


        private LinearViewHolder(View itemView) {
            super(itemView);
            frequencyNo = itemView.findViewById(R.id.frequency_name);
            frequencyOnline = itemView.findViewById(R.id.frequency_show_online);
        }
    }
}
